package com.gls.security.form.login.configurer.adapter;

import com.gls.security.core.configurer.adapter.HttpSecurityConfigurerAdapter;
import com.gls.security.form.login.properties.FormLoginConstants;
import com.gls.security.form.login.properties.FormLoginProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

/**
 * @author lizhiyong
 */
@Component
public class FormLoginConfigurerAdapter implements HttpSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    private FormLoginProperties formLoginProperties;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        configure(http.formLogin());
    }

    private void configure(FormLoginConfigurer<HttpSecurity> configurer) {
        configurer
                .loginPage(FormLoginConstants.DEFAULT_UNAUTHENTICATION_URL)
                .loginProcessingUrl(formLoginProperties.getLoginProcessingUrl())
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler);
    }
}
